import sys
import subprocess
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QLineEdit, QPushButton, QTextEdit, QAction, QFileDialog, QMessageBox
from PyQt5.QtCore import QProcess, QByteArray, QIODevice, pyqtSlot


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Windows应用运行工具")
        self.setGeometry(100, 100, 500, 400)

        self.exe_path = ""
        self.output_text = ""

        self.create_menu()
        self.create_widgets()
        self.create_layout()

    def create_menu(self):
        menu_bar = self.menuBar()
        help_menu = menu_bar.addMenu("帮助")

        about_action = QAction("关于", self)
        about_action.triggered.connect(self.show_about_dialog)
        help_menu.addAction(about_action)

    def create_widgets(self):
        self.exe_label = QLabel("EXE 路径:")
        self.exe_textbox = QLineEdit()
        self.exe_button = QPushButton("选择 EXE")
        self.exe_button.clicked.connect(self.select_exe)

        self.run_button = QPushButton("运行 EXE")
        self.run_button.clicked.connect(self.run_exe)

        self.install_button = QPushButton("安装 Wine兼容环境")
        self.install_button.clicked.connect(self.install_wine)

        self.output_label = QLabel("输出:")
        self.output_textbox = QTextEdit()
        self.output_textbox.setReadOnly(True)

    def create_layout(self):
        main_layout = QVBoxLayout()

        exe_layout = QHBoxLayout()
        exe_layout.addWidget(self.exe_label)
        exe_layout.addWidget(self.exe_textbox)
        exe_layout.addWidget(self.exe_button)

        main_layout.addLayout(exe_layout)
        main_layout.addWidget(self.run_button)
        main_layout.addWidget(self.install_button)
        main_layout.addWidget(self.output_label)
        main_layout.addWidget(self.output_textbox)

        central_widget = QWidget()
        central_widget.setLayout(main_layout)
        self.setCentralWidget(central_widget)

    def select_exe(self):
        file_dialog = QFileDialog()
        file_dialog.setFileMode(QFileDialog.ExistingFile)
        file_dialog.setNameFilter("Executable Files (*.exe)")
        if file_dialog.exec_():
            selected_files = file_dialog.selectedFiles()
            self.exe_path = selected_files[0]
            self.exe_textbox.setText(self.exe_path)

    def run_exe(self):
        if not self.exe_path:
            QMessageBox.warning(self, "错误！", "请选择EXE可执行文件.")
            return

        self.output_textbox.clear()
        self.output_text = ""

        self.process = QProcess()
        self.process.readyReadStandardOutput.connect(self.read_output)
        self.process.readyReadStandardError.connect(self.read_output)
        self.process.finished.connect(self.process_finished)

        self.process.start("wine", [self.exe_path])

    def install_wine(self):
        self.output_textbox.clear()
        self.output_text = ""

        self.process = QProcess()
        self.process.readyReadStandardOutput.connect(self.read_output)
        self.process.readyReadStandardError.connect(self.read_output)
        self.process.finished.connect(self.process_finished)

        self.process.start("pkexec", ["apt-get", "install", "-y", "wine"])

    @pyqtSlot()
    def read_output(self):
        data = self.process.readAllStandardOutput()
        self.output_text += data.data().decode()
        self.output_textbox.setText(self.output_text)

        error = self.process.readAllStandardError()
        self.output_text += error.data().decode()
        self.output_textbox.setText(self.output_text)

    @pyqtSlot()
    def process_finished(self):
        self.process.kill()

    def show_about_dialog(self):
        QMessageBox.about(self, "关于", "这是一个Windows应用程序运行工具，以Wine为核心，专为梦心操作系统开发。\n\n版权所有:中宏软件")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

